
using System;
using System.Collections.Generic;
using M0;
using Newtonsoft.Json;
using UnityEngine;

namespace M1
{
    public class BaseModel: ScriptableObject
    {
        public virtual void editor检查数据()
        {
            Debug.Log("检查数据");
        }
    }
    public class BaseModel<M,T> :BaseModel  where M : BaseModel
    {
        private static M instance;
        public static M Instance{get{
                if (instance == null)
                {
                    instance = Model管理器.Instance.GetModel<M>();
                }
                return instance;
            }
        }
    
        [SerializeField] protected string 配置路径;
        [SerializeField] protected List<T> Datas;

#if UNITY_EDITOR
        public void AddData(List<T> ddd)
        {
            Datas.AddRange(ddd);
        }
#endif
        
        public List<T> GetData()
        {
            return Datas.CopyList();
        }
        public virtual void Init()
        {
            
        }
    }

    
    //缓存数据================================================
    public class Base数据缓存<T> where T :I缓存<T>,new()
    {
        protected static T instance;
        public static T Instance{get{
                if (instance == null)
                {
                    string key = typeof(T) + "Hc";
                    instance = M.Get缓存<T>(key);
                    if (instance == null)
                    {
                        instance = new T();
                        instance.Init();
                        M.Event.AddEvent(M.EventKey_游戏结束,instance.及时保存数据);
                    }else if (instance.GetData() == null)
                    {
                        instance.Init();
                        M.Event.AddEvent(M.EventKey_游戏结束,instance.及时保存数据);
                    }
                }
                return instance;
            }
        }

        public virtual void 及时保存数据()
        {
            if(instance==null) return;
            
            string key = typeof(T) + "Hc";
            M.保存缓存(key,JsonConvert.SerializeObject(instance.GetData()));
        }
    }

    public interface I缓存<D>
    {
        void Init();
        D GetData();

        void 及时保存数据();
    }
}

